package com.dbManage.page.essay;

import com.dbManage.common.service.TagService;
import com.dbManage.dao.EssayDao;
import com.dbManage.entity.Message;
import com.dbManage.entity.Note;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class EssayController {
    @Resource
    EssayService essayService;
    @Resource
    TagService tagService;
    @Resource
    EssayDao essayDao;

    /**
     * 随笔列表
     * @param model
     * @return
     */
    @RequestMapping("/essayList")
    public String noteList(Model model) {
        model.addAttribute("tagMap",tagService.getTagMap());
        model.addAttribute("list", essayService.queryEssay(null));
        return "essayList";
    }

    /**
     * 随笔筛选
     * @param note
     * @return
     */
    @RequestMapping("/queryEssay")
    @ResponseBody
    public Message queryEssay(Note note){
        List<Note> list=essayService.queryEssay(note);
        return new Message(Message.STATUS_SUCCESS,list,"success");
    }

    /**
     * 添加随笔页面
     * @param model
     * @return
     */
    @RequestMapping("/addEssayView")
    public String addEssayView(Model model) {
        model.addAttribute("tagMap",tagService.getTagMap());
        return "essayAdd";
    }

    /**
     * 添加随笔
     * @param note
     * @return
     */
    @RequestMapping("/addEssay")
    @ResponseBody
    public Message addEssay(Note note) {
        essayService.addEssay(note);
        return new Message(Message.STATUS_SUCCESS, "添加成功");
    }

    /**
     * 更新随笔页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/updateEssayView")
    public String updateEssayView(Model model,int id) {
        model.addAttribute("note",essayDao.findEssayById(id));
        model.addAttribute("tagMap",tagService.getTagMap());
        return "essayEdit";
    }

    /**
     * 更新随笔
     * @param note
     * @return
     */
    @RequestMapping("/updateEssay")
    @ResponseBody
    public Message updateEssay(Note note) {
        essayService.updateEssay(note);
        return new Message(Message.STATUS_SUCCESS, "修改成功");
    }

    /**
     * 删除随笔
     * @param id
     * @return
     */
    @RequestMapping("/deleteEssay")
    @ResponseBody
    public Message deleteEssay(int id){
        essayDao.deleteEssayById(id);
        return new Message(Message.STATUS_SUCCESS,"删除成功");
    }

}
